package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.MesaBean;

import clienteWeb.ControladorWeb;

public class ObtenerMesasMozo extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
    public ObtenerMesasMozo() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		    response.setContentType("text/html");
		    PrintWriter out = response.getWriter();
		    String title = "Apertura de Mesa";
		    
		    List<MesaBean> mesas=ControladorWeb.getInstancia().getMesasDeMozo(Integer.parseInt(request.getParameter("cbMozos")));
		    		    
		    String HTML="<%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\"\n" +
		    	    	"pageEncoding=\"ISO-8859-1\"%>" +
		    	    	"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
		    	    	"<html>\n" +
						"<head>\n" +
						"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" + 
						"<title>" + title + "</title>\n" +
						"<script type=\"text/javascript\">\n" +
						"function validarForm(){\n" +
						"if (document.getElementById(\"comboMesas\").value==\"\") {\n" + 
						"alert('Debe ingresar una Mesa.');" +
						"return false;\n" +
						"}\n\n" +
						"if(document.getElementById(\"txtComensales\").value == \"\") {\n" +
						"alert('Debe ingresar una cantidad de comensales.');\n" +
						"return false;\n" +
						"}\n" +
						"document.getElementById(\"formularioMesas\").submit();\n" +
						"}\n" +
						"</script>\n" +
						"</head>\n";
				
			HTML=HTML +	"<body>\n" +
						"<div>\n" +
						"<h2>" + title + "</h2>\n" +
						"</div>\n" +
						"<form name=\"frmMuestraMesas\" id=\"formularioMesas\" method=\"GET\" action=\"AbrirMesa\">\n" +
						"<blockquote>\n" +
						"<h3>Mesas:\n" +
						"<select name=\"cbMesas\" id=\"comboMesas\" size=\"1\">\n";
										    
				    	for (MesaBean mesa : mesas) {
				    		HTML=HTML + "<option value=\"" + mesa.getNumero().toString() + "\">\n" +
					            "Mesa " + mesa.getNumero().toString() +					            
					            "</option>\n";
				    	}					    
					    
			HTML=HTML + "</select>\n" +
		    			"</h3><br/>\n" +						
					    "Cantidad Comensales: <input type=\"text\" id=\"txtComensales\" name=\"txtCantComensales\">\n" +
					    "<input type=\"hidden\" name=\"hdMozo\" id=\"hiddenMozo\" value=\"" + request.getParameter("cbMozos") + "\">\n" +
					    "<input name=\"btnAbrirMesas\" type=\"button\" value=\"Abrir Mesa\" onClick=\"validarForm();\">\n" +
					    "</blockquote>\n" +					    
					    "</form>\n" +    
					    "</body>\n" +
					    "</html>";
					    
			out.println(HTML);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
}
